/**
 * @Description: 公共状态-全局状态
 * @Author: zhen
 * @Date: 2023-10-17
 */

import { defineStore } from 'pinia';
import { GlobalState, ThemeConfigProps, ElementSizeType } from '../types';
import { DEFAULT_PRIMARY } from '@/constants';
import piniaPersistConfig from '@/plugins/pinia-persist';

// defineStore 调用后返回一个函数，调用该函数获得 Store 实体
const useGlobalStore = defineStore({
  // id: 必须的，在所有 Store 中唯一
  id: 'GlobalState',
  // state: 返回对象的函数
  state: (): GlobalState => ({
    // token
    token: '',
    // userName
    userName: '',
    // element组件大小
    elementSize: 'default',
    // language
    language: '',
    // themeConfig
    themeConfig: {
      // 默认 primary 主题颜色
      primary: DEFAULT_PRIMARY,
      // 橙色模式
      isOrange: true,
      // 灰色模式
      isYellow: false,
      // 蓝色模式
      isBlue: false
    }
  }),
  getters: {},
  actions: {
    // setToken
    setToken(token: string) {
      this.token = token;
    },
    // setUserName
    setUserName(userName: string) {
      this.userName = userName;
    },
    // setAssemblySizeSize
    setAssemblySizeSize(elementSize: ElementSizeType) {
      this.elementSize = elementSize;
    },
    // updateLanguage
    updateLanguage(language: string) {
      this.language = language;
    },
    // setThemeConfig
    setThemeConfig(themeConfig: ThemeConfigProps) {
      this.themeConfig = themeConfig;
    }
  },
  persist: piniaPersistConfig('GlobalState')
});

export default useGlobalStore;
